package com.example.hackercore.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Description：
 *
 * @Author: cw
 * CreateTime：2022-10-21
 */
public class LeetCode {


    //两数之和
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            int j = target - nums[i];
            if (map.containsKey(j)) {
                return new int[]{map.get(j), i};
            }
            map.put(nums[i], i);
        }
        return null;
    }

    //两数相加
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode();
        ListNode cum = pre;
        int curry = 0;
        while (l1 != null || l2 != null) {
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;

            int sum = x + y + curry;

            curry = sum / 10;
            sum %= 10;

            cum.next = new ListNode(sum);
            cum = cum.next;

            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (curry == 1) {
            cum.next = new ListNode(1);
        }
        return pre.next;
    }

    //无重复字符的最长子串
    public static int lengthOfLongestSubstring(String s) {
        int x = 0;
        int y = 0;
        int z = 0;
        for (int i = 0; i < s.length(); i++) {
            int po = s.indexOf(s.charAt(i), x);
            if (po < i) {
                if (z > y) {
                    y = z;
                }
                if (y >= s.length() - po - 1) {
                    return y;
                }
                z = i - po - 1;
                x = po + 1;
            }
            z++;
        }
        return z;
    }

    //寻找两个正序数组的中位数
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        if (nums1.length == 0 && nums2.length == 0) {
            return 0;
        }
        double x = 0d;
        int[] nums = new int[nums1.length + nums2.length];

        System.arraycopy(nums1, 0, nums, 0, nums1.length);
        System.arraycopy(nums2, 0, nums, nums1.length, nums2.length);
        Arrays.sort(nums);

        int i = nums.length % 2;

        int y = nums.length / 2;
        if (i == 0) {
            x = (nums[y - 1] + nums[y]) / 2d;
        } else {
            x = nums[y];
        }
        return x;
    }

    //最长回文子串
    public String longestPalindrome(String s) {
        int left = 0;
        int right = 0;
        int len = s.length();
        int[][] dp = {};
        for (int i = 1; i < len; i++) {

        }


        return null;
    }


    public static void main(String[] args) {
        System.out.println(findMedianSortedArrays(new int[]{1, 2}, new int[]{3, 4}));
    }


}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

}
